package it.polimi.entities.web;

import java.util.ArrayList;
import java.util.List;

import it.polimi.entitiesDTO.AttractionDTO;
import it.polimi.entitiesManagers.AttractionMgr;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean(name = "AttractionBean")
@SessionScoped
public class AttractionBean {
	


		@EJB
		private AttractionMgr amgr;

		private AttractionDTO attractionDTO;
		
		private AttractionDTO attractionDTO2;
		
		private List<String> cityAttraction;
		
		private String city;
		
		private int id;
		
		private List<Integer> list = new ArrayList<Integer>();

		
		/* --------------------------
		 * -----CLASS METHODS--------
		 * --------------------------
		 */
		
		public String remove(){
			this.list = amgr.getAllId();
			if(this.list.contains(this.id))
				{amgr.remove(this.id);
				clearAll();
				return "/admin/index";
				}
			else return "/admin/errorPage";
		}
		
		public void clearAll(){
			this.attractionDTO = new AttractionDTO();
			
			this.setAttractionDTO2(new AttractionDTO());
			
			this.cityAttraction = new ArrayList<String>();
			
			this.city = new String();
			
			this.id=0;
			
			this.list = new ArrayList<Integer>();
		}
		
		public AttractionBean() {
			clearAll();
		}
		
		public String register() {
			amgr.save(this.attractionDTO);
			return "index?faces-redirect=true";
			// "index?faces-redirect=true";
		}
		
		public void fillCityAttraction(){
			this.cityAttraction=amgr.getAttractionByCity(city);
			list= amgr.getAllId();
		}
		
		public String modifyAttraction(){
			if (list.contains((Integer)id))
			{ 	attractionDTO2.setID(id);
				amgr.update(attractionDTO2);
				clearAll();
				return "index?faces-redirect=true";
			}
			return "/Admin/errorPage";	
			
		}
		
		
		
		/* --------------------------
		 * ----SETTERS & GETTERS-----
		 * --------------------------
		 */
		
		public AttractionDTO getAttractionDTO() {
			return attractionDTO;
		}

		public void setAttractionDTO(AttractionDTO attractionDTO) {
			this.attractionDTO = attractionDTO;
		}

		public int getId(){
			return this.id;
		}
		
		public void setId(int id){
			this.id=id;
		}
		public AttractionDTO getAttractionDTO2() {
			return attractionDTO2;
		}

		public void setAttractionDTO2(AttractionDTO attractionDTO2) {
			this.attractionDTO2 = attractionDTO2;
		}

		public String getCity() {
			return city;
		}

		public void setCity(String city) {
			this.city = city;
		}

		public List<String> getcityAttraction() {
			return cityAttraction;
		}

		public void setcityAttraction(List<String> cityAttraction) {
			this.cityAttraction = cityAttraction;
		}

}
